package com.example.demo.controllerApi;

import cn.dev33.satoken.basic.SaBasicUtil;
import cn.dev33.satoken.stp.StpUtil;
import cn.dev33.satoken.util.SaResult;
import com.example.demo.annotation.IgnoreResponseAdvice;
import com.example.demo.entity.UserInfoTest;
import com.example.demo.util.AjaxJson;
import com.example.demo.util.CustomException;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;

/**
 * @Author: Pinp
 * @Date: 2021/7/21 11:30
 */

@Slf4j
@RestController
@RequestMapping("/api")
public class ApiTests {

    @RequestMapping("test1")
    public AjaxJson test1(HttpServletRequest request) {
        log.info("Do Something");
        String ipAddress = request.getRemoteAddr();
        return AjaxJson.getSuccess(ipAddress);
    }

    @RequestMapping("test2")
    public AjaxJson test2() {
        log.info("Do Something");
        return AjaxJson.getByBoolean(true);
    }

    @RequestMapping("test1/time")
    public AjaxJson test1t(@RequestParam Timestamp time) {      // Timestamp format must be yyyy-mm-dd hh:mm:ss[.fffffffff]
        log.info(String.valueOf(time));
        return AjaxJson.getByBoolean(true);
    }

    @RequestMapping("test1/login")
    public AjaxJson test1l() {
        return AjaxJson.getSuccess(String.valueOf(StpUtil.isLogin()));
    }

    @RequestMapping("test2/login")
    public AjaxJson test2l() {
        return AjaxJson.getSuccess(String.valueOf(StpUtil.isLogin()));
    }

    @PostMapping("test1/user")
    public AjaxJson test1u(@RequestBody UserInfoTest ui) {
        log.info(ui.toString());
        return AjaxJson.getSuccess();
    }

    @PostMapping("test2/user")
    public AjaxJson test2u(@RequestBody UserInfoTest ui, @RequestParam String x) {
        log.info(ui.toString());
        log.info(x);
        return AjaxJson.getSuccess();
    }

    @IgnoreResponseAdvice
    @RequestMapping("test1/auth")
    public AjaxJson test1auth() {
        log.info("x1");
        SaBasicUtil.check("test1:12345678");
        log.info("x2");
        return AjaxJson.getSuccess();
    }

    @RequestMapping("test1/authx")
    public SaResult test1authX() {
        log.info("x3");
        SaBasicUtil.check("test2:12345678");
        log.info("x4");
        return SaResult.ok();
    }

    @SneakyThrows
    @RequestMapping("test3/x")
    public SaResult test3(String arg) {
        log.info("test");
        if (arg == null){
            throw new CustomException("Some Wromg");
        }
        return SaResult.ok();
    }

}
